Python Programming Guide For Beginners: A Simple Introduction to Python Programming by Phillips Gerald
Author:Phillips, Gerald [Phillips, Gerald]
Language: eng
Format: epub
Publisher: UNKNOWN
Published: 2019-10-08T16:00:00+00:00
Lists
This is a sequence type defined by the list class in Python and it allows you to add, delete, and process elements easily.
Creating a list
Syntax: >>> my_list = [1,2,3,4,5]
Elements in a list can be of the same or different types. For example,
mixed_list = [“my mixed list”, 12, 33.54]
Other ways of creating a list are as follows:
list1 = list() # Create an empty list
list2 = list([22, 31, 61]) # Create a list with elements 22, 31, 61
list3 = list(["tom", "jerry", "spyke"]) # Create a list with strings
list5 = list("python") # Create a list with characters p, y, t, h, o, n
NOTE: Lists are mutable , meaning their content can be changed once created.
Accessing List Elements
List elements can be accessed using the index operator ( [] ), with the index starting from 0.
>>> my_list = [1,2,3,4,5]
>>> my_list [1] # access second element in the list 2
>>> my_list [0] # access first element in the list 1
Common List Operations
Method Name Description
x in s True if element x is in sequence s
x not in s True if element x is not in sequence s
s1 + s2 Concatenates two sequences s1 and s2
s * n , n * s n copies of sequence s concatenated
s[i] ith element in sequence s
len(s) Length of sequence s, i.e. the number of elements in s
min(s) Smallest element in sequence s
max(s) Largest element in sequence s
sum(s) Sum of all numbers in sequence s
for loop Traverses elements from left to right in a for loop
List Slicing
The slice operator ( [start:end] ) allows to fetch sublist from the list. It works similar to string explained in the previous chapter.
Note: If start >= end , list[start : end] will return an empty list. If end specifies a position which is beyond the end of the list, Python will use the length of the list for end instead.
Commonly Used List Methods with Return Type
Method Name Description
append(x:object):None
Adds an element x to the end of the list and returns None
count(x:object):int
Returns the number of times element x appears in the list
extend(l:list):None
Appends all the elements in l to the list and returns None
index(x: object):int
Returns the index of the first occurrence of element x in the list
insert(index: int, x: object):None
Inserts an element x at a given index. Note that the first element in the list has index 0 and returns None
remove(x:object):None
Removes the first occurrence of element x from the list and returns None
reverse():None Reverse the list and returns None
sort(): None
Sorts the elements in the list in ascending order and returns None
spop(i): object
Removes the element at the given position and returns it. The parameter i is optional. If it is not specified, pop() removes and returns the last element in the list.
List Comprehension
Note: This topic needs to have a working knowledge of python for loops.
List comprehension provides a concise way to create lists. It consists of square brackets containing an expression followed by a for clause then zero or more if clauses.
Below are some examples:
>>> list1 = [ x for x in range(10) ] >>> list1
[0, 1, 2, 3, 4,
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Deep Learning with Python by François Chollet(12568)
Hello! Python by Anthony Briggs(9912)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9795)
The Mikado Method by Ola Ellnestam Daniel Brolund(9777)
Dependency Injection in .NET by Mark Seemann(9336)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8293)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7758)
Grails in Action by Glen Smith Peter Ledbrook(7693)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7040)
Microservices with Go by Alexander Shuiskov(6805)
Practical Design Patterns for Java Developers by Miroslav Wengner(6717)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6656)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6409)
Angular Projects - Third Edition by Aristeidis Bampakos(6065)
The Art of Crafting User Stories by The Art of Crafting User Stories(5596)
NetSuite for Consultants - Second Edition by Peter Ries(5531)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5335)
Kotlin in Action by Dmitry Jemerov(5062)
